using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sfcms.api;
using System.Text;

public partial class sfcms_sfcms_jsserver : webpagebase
{
    protected override void Page_Load()
    {
        string action = Request.Form["action"];
        string treeViewName = Request.Form["treeView.name"];
        string nodeKey = Request.Form["node.key"];
        bool isWebPageManagement = Request.Form["folder.type"] == "1";
        bool isSelectOnly = Request.Form["sel"] == "1";
        int rowSize = isWebPageManagement ? 24 : file.THUMBNAIL_SIZE + 4;

        string[] nodePath = nodeKey.Split('/');

        long parentFolderID = Convert.ToInt64(nodePath[0]);
        
        folder parentFolder;
        if (parentFolderID == 0)
        {
            parentFolder = isWebPageManagement ? folder.pageroot : folder.fileroot;
        }
        else
        {
            parentFolder = folder.byid(parentFolderID);
        }

        bool isNewFolderRequest = nodeKey.EndsWith("/0");

        switch (action)
        {
            case "EXPAND":

                Response.Write(string.Format("var myNode = {0}.Item(\"{1}\");", treeViewName, nodeKey));

                Response.Write(string.Format("{0}.isInRunTimeMode = false;", treeViewName));
                Response.Write("myNode.Nodes.Clear();");

                folder[] childFolders = folder.byparentid(parentFolderID, isWebPageManagement ? 1 : 2);
                
                foreach (folder childFolder in childFolders)
                {
                    Response.Write(string.Format("{0}.Add(\"{1}\", \"{2}\", \"{3}\", null, 0, false);", treeViewName, nodeKey, childFolder.id, childFolder.displayname));
                    Response.Write(string.Format("{0}.Add(\"{2}\", \"{2}/TEMP\", \"\", null, 0, false);", treeViewName, nodeKey, childFolder.id));
                }

                if (!isSelectOnly)
                {
                    Response.Write(string.Format("{0}.Add(\"{1}\", \"{1}/0\", \"Add\", null, 2, false);", treeViewName, nodeKey));
                }
                Response.Write(string.Format("{0}.isInRunTimeMode = true;", treeViewName));
                Response.Write(string.Format("{0}.Refresh();", treeViewName));

                break;

            case "CLICK":

                if (!isNewFolderRequest)
                {
                    StringBuilder htmlBuilder = new StringBuilder();
                    htmlBuilder.Append("<table style='width:95%;margin-left:5px;margin-top:3px;' cellpadding='0' cellspacing='0' border='0'><tr><td style='height:20px;border-bottom:2px solid black;'>&nbsp;<b>Items of " + parentFolder.name + "<b></td></tr></table>");
                    // page currentpage = page.byid(Convert.ToInt64(nodeKey)); // XXXXX
                    child[] children;
                    if (isWebPageManagement)
                    {
                        children = page.byparentid(parentFolderID);
                    }
                    else
                    {
                        children = file.byparentid(parentFolderID);
                    }

                    htmlBuilder.Append("<table style='width:95%;margin-left:5px;margin-top:3px;' cellpadding=0 cellspacing=0 border=0>");
                    
                    foreach (child item in children)
                    {
                        htmlBuilder.Append("<tr><td style='border-bottom:1px solid #e0e0e0;height:" + rowSize + "px;width:" + rowSize + "px;'>");
                        if(item is page)
                        {
                            htmlBuilder.Append("<img src='/web/sfcms/i/treeview/icons/item.gif'/>");
                        }
                        else
                        {
                            if ((item as file).isimage)
                            {
                                htmlBuilder.Append("<img src='/web/sfcms/sfcms_download.aspx?id=" + item.id + "&th=1' />");
                            }
                            else
                            {
                                htmlBuilder.Append("<img src='/web/sfcms/i/treeview/icons/item.gif'/>");
                            }
                        }
                        htmlBuilder.Append("</td><td style='border-bottom:1px solid #e0e0e0;'>");
                        
                        if(!isWebPageManagement && isSelectOnly)
                        {
                            htmlBuilder.Append("<a href='javascript:selectImage(" + item.id + ");'");
                        }
                        else
                        {
                            htmlBuilder.Append("<a href='/web/sfcms/");
                            htmlBuilder.Append(isWebPageManagement ? "sfcms_page" : "sfcms_file");
                            htmlBuilder.Append(".aspx?key=" + nodeKey + "/" + item.id + "'");
                        }
                        
                        htmlBuilder.Append(">" + (isWebPageManagement ? (item as page).displayname : (item as file).filename + "." + (item as file).fileextension) + "</td></tr>");
                    }

                    if (isWebPageManagement || !isSelectOnly)
                    {
                        htmlBuilder.Append("<tr><td style='height:" + rowSize + "px;width=" + rowSize + "px;'><img src='/web/sfcms/i/treeview/icons/item_add.gif'/></td><td>");
                        htmlBuilder.Append("<a href='/web/sfcms/");
                        htmlBuilder.Append(isWebPageManagement ? "sfcms_page" : "sfcms_file");
                        htmlBuilder.Append(".aspx?key=" + nodeKey + "/0");
                        htmlBuilder.Append("'>Add</a></td></tr>");
                    }
                    htmlBuilder.Append("</table>");
                    
                    Response.Write("document.getElementById('items').innerHTML = \"" + htmlBuilder.ToString() + "\";");
                }
                else
                {
                    Response.Write("document.getElementById('folderproperties').style.display='inline';");
                    Response.Write("document.getElementById('foldernodekey').value = '" + nodeKey + "';");
                    Response.Write("document.getElementById('foldername').value = '';");
                    Response.Write("document.getElementById('folderdisplayname').value = '';");
                }
                break;

            case "SAVE":

                string folderName = Request.Form["foldername"];
                string folderDisplayName = Request.Form["folderdisplayname"];
                folder newFolder = new folder();
                newFolder.name = folderName;
                newFolder.displayname = folderDisplayName;
                newFolder.parentid = parentFolderID;
                newFolder.type = isWebPageManagement ? 1 : 2;
                newFolder.save();

                Response.Write("cancelfolder();");
                nodeKey = nodeKey.Replace("/0", "");
                Response.Write(string.Format("var myNode = {0}.Item(\"{1}\");", treeViewName, nodeKey));

                Response.Write(string.Format("{0}.isInRunTimeMode = false;", treeViewName));
                Response.Write("myNode.Nodes.Clear();");

                folder[] newfolderlist = folder.byparentid(parentFolderID, isWebPageManagement ? 1 : 2);

                foreach (folder childfolder in newfolderlist)
                {
                    Response.Write(string.Format("{0}.Add(\"{1}\", \"{2}\", \"{3}\", null, 0, false);", treeViewName, nodeKey, childfolder.id, childfolder.displayname));
                    Response.Write(string.Format("{0}.Add(\"{1}\", \"{1}/TEMP\", null, null, 0, false);", treeViewName, childfolder.id));
                }
                Response.Write(string.Format("{0}.Add(\"{1}\", \"{1}/0\", \"Add\", null, 2, false);", treeViewName, nodeKey));
                Response.Write(string.Format("{0}.isInRunTimeMode = true;", treeViewName));
                Response.Write(string.Format("{0}.Refresh();", treeViewName));

                break;

            case "DELETE":

                break;

            default:
                Response.Write("alert(\"Invalid command!\");");
                break;
        }

        // Response.Write("alert(\"" + Request.Form["action"] + " / " + Request.Form["node.key"] + "\");");

    }
}
